//
//  TitleView.m
//  TodatPlay
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "TitleView.h"
#import "TAMacro.h"
#import "NetEaseTitleLayout.h"
#import "TitleCollectionViewCell.h"
#import "ClassifyCategorydoc.h"
#import "UIView+Frame.h"
#define kCollectionTitleViewReuse @"titleReuse"
@interface TitleView ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, NetEaseTitltLayoutDelegate>

@property (nonatomic, strong) NetEaseTitleLayout *layout;
//@property (nonatomic, copy)CellWidth width;

@end

@implementation TitleView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)createCollectionView {
    self.layout = [[NetEaseTitleLayout alloc] init];
    _layout.titleFont = [UIFont systemFontOfSize:17];
    _layout.delegate = self;
    self.titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) collectionViewLayout:_layout];
    _titleCollectionView.backgroundColor = [UIColor whiteColor];
    _titleCollectionView.showsHorizontalScrollIndicator = YES;
    _titleCollectionView.delegate = self;
    _titleCollectionView.dataSource = self;
    [self addSubview:_titleCollectionView];
    [self.titleCollectionView registerNib:[UINib nibWithNibName:@"TitleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionTitleViewReuse];
    self.titleCollectionView.bounces = NO;
    self.titleCollectionView.showsHorizontalScrollIndicator = NO;
    [self createLineView];

}
//- (void)cellWidth:(CellWidth)width {
//    _width = width;
//}
- (void)createLineView {
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 37, 20, 3)];
    [self addSubview:_lineView];
    _lineView.backgroundColor = [UIColor blackColor];
}
- (void)setCategoryArr:(NSArray<ClassifyCategorydoc *> *)categoryArr {
    _categoryArr = categoryArr;

    [self.titleCollectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _categoryArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionTitleViewReuse forIndexPath:indexPath];
    cell.titleLabel.text = _categoryArr[indexPath.row].name;
    cell.titleLabel.textAlignment = NSTextAlignmentCenter;
    cell.titleLabel.font = _layout.titleFont;
    
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout string:(NSString *)string stringFont:(UIFont *)font widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = _categoryArr[indexPath.row].name;
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layout.titleFont}
                      
                                       context:nil];
    
    return newRect.size.width + 0.5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    CGFloat currentCenterX = 0;
    for (int i = 0; i < indexPath.row; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[collectionView cellForItemAtIndexPath:index];
        CGFloat cellWidth = cell.contentView.dm_width;
        currentCenterX += cellWidth;
        
    }

    self.lineView.dm_width = cell.contentView.dm_width;
    
    self.lineView.dm_centerX = currentCenterX + cell.contentView.dm_width / 2.0;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
